<?php

namespace App\Http\Controllers\product;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use DB;

class SalesController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $where =[];

        $ob=DB::table('sales');
         if($request->has('product_id')){
            // 获取要搜索的name字段的值
            $id = $request->input('product_id');
            //添加where模糊匹配条件
            $ob->where('product_id','like',"%{$id}%");
            $where['product_id'] = $id;
        }
        $list=$ob->paginate(5);
        return view('sales.index',['list'=>$list]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {   
        $pr=DB::table('product')->get()->all();
        return view('sales.add',['pr'=>$pr]);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        // 自动规则
        $message=[
        'required'=>':attrbute 不能为空' 
        ];
        $this->validate($request,[
            'product_id'=>'required'],$message

            );
        $data = $request->except('_token');
        $db=DB::table('sales')->insertGetId($data);
        if($db>0){
             return redirect('/admin/sales')->with('msg','添加成功');
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $db=DB::table('sales')->where('id',$id)->first();
        return view('sales.edit',['list'=>$db]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
         $message=[
        'required'=>':attrbute 不能为空' 
        ];
        $this->validate($request,[
            'product_id'=>'required'],$message

            );
        $data = $request->except('_token');
        $db=DB::table('sales')->where('id',$id)->update($data);
        if($db>0){
             return redirect('/admin/sales')->with('msg','添加成功');
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $db=DB::table('sales')->where('id',$id)->delete();
        if($db>0){
             return redirect('admin/sales')->with('msg','删除成功');
        }
    }
}
